﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TimKen.DataEntity;
using System.Data;
using System.Data.SqlClient;
namespace TimKen.DataAccess
{
  public  class AdvertisementDAO
    {
      /// <summary>
      /// 添加图片  
      /// </summary>
      /// <param name="adv">图片实体</param>
      /// <returns></returns>
      public bool InsertImage(Advertisement adv)
      {
          bool bo=false;
          string sql = "insert into Advertisement(ImagePath,Url,Oby,Imgtype,pp)  values('{0}','{1}','{2}','{3}','{4}')";
          int i = DbHelperSQL.ExecuteSql(string.Format(sql, adv.ImagePath, adv.Url, adv.Oby, adv.Imgtype, adv.PP == "" ? "" : adv.PP));
         if (i > 0)
         {
             bo = true;
         }
         else {
             bo = false;
         }
         return bo;
      }
      /// <summary>
      /// 分页管理广告
      /// </summary>
      /// <param name="pageSize">每页大小</param>
      /// <param name="curtIndex">当前页</param>
      /// <returns></returns>
      public DataSet GetAdvertisement(int ImgTyid, int pageSize, int curtIndex)
      { 
           SqlParameter[] parameters = {
                    new SqlParameter("@ImgTyid",SqlDbType.Int,4),
					new SqlParameter("@pageSize", SqlDbType.Int,4),
					new SqlParameter("@cutPageIndex", SqlDbType.Int,4)};
                  parameters[0].Value = ImgTyid;
                  parameters[1].Value = pageSize;
                  parameters[2].Value = curtIndex;
                  DataSet ds = DbHelperSQL.RunProcedure("UP_Advertisement", parameters, "ds");
                if (ds.Tables[0].Rows.Count > 0)
                {
                    return ds;
                }
                else {
                    return new DataSet();
                }
      }
      /// <summary>
      /// 根据id得到当条记录
      /// </summary>
      /// <param name="id">id</param>
      /// <returns></returns>
      public DataSet getAdvertisementByID(string id)
      {
          string sql = "select * from Advertisement where id='{0}'";
          DataSet dt = DbHelperSQL.GetDataSet(string.Format(sql, id));
          if (dt.Tables.Count > 0)
          {
              return dt;
          }
          else
          {
              return null;
          }
      }
      /// <summary>
      /// 修改记录
      /// </summary>
      /// <param name="adv">广告对象</param>
      /// <returns></returns>
      public bool updateAdvertisement(Advertisement adv)
      {
          bool bo = false;
          string sql = "update Advertisement set Url='{0}',Oby='{1}'";
          if(adv.ImagePath!=null&&adv.ImagePath!="")
          {
              sql += ",ImagePath='"+adv.ImagePath+"'";
          }
          if (adv.PP != null && adv.PP != "")
          {
              sql += ",pp='"+adv.PP+"'";
          }
          sql+="  where id='{2}'";
          int i = DbHelperSQL.ExecuteSql(string.Format(sql, adv.ImagePath, adv.Oby,adv.Id));
          if (i > 0)
          {
              bo = true;
          }
          else
          {
              bo = false;
          }
          return bo;
      }
      /// <summary>
      /// 删除当前记录
      /// </summary>
      /// <param name="id">id号</param>
      /// <returns></returns>
      public bool Delete(string id)
      {
          bool bo = false;
          string sql = "delete from Advertisement where id='{0}'";
          int i = DbHelperSQL.ExecuteSql(string.Format(sql,id));
          if (i > 0)
          {
              bo = true;
          }
          else
          {
              bo = false;
          }
          return bo;
      }
      public bool ExecOrder(string id1, string id2)
      {
          SqlParameter[] parameters = {
					new SqlParameter("@id1", SqlDbType.VarChar,10),
					new SqlParameter("@id2", SqlDbType.VarChar,10)};
          parameters[0].Value = id1;
          parameters[1].Value = id2;
          int i=0;
          int ds = DbHelperSQL.RunProcedure("UP_UpdateOrder", parameters,out i);
          if(i>0)
          {
            return true;
          }else{
              return false;
          }
          
      }
      /// <summary>
      /// 设置排序
      /// </summary>
      /// <returns></returns>
         public int setOrderNum()
        {
            string sql = "select max(Oby) from Advertisement";
            string str=  DbHelperSQL.GetData(sql);
            if (str == null || str == "")
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(str) + 1;
            }
        }
      /// <summary>
      /// 得到当前显示的个数
      /// </summary>
      /// <returns></returns>
         public int getIsShowNum(string i)
         {
             string sql = "select count(*) from Advertisement where isShow=1 and Imgtype='{0}'";
             string str = DbHelperSQL.GetData(string.Format(sql,i));
             return Convert.ToInt32(str);
         }
      /// <summary>
      /// 修改状态
      /// </summary>
      /// <param name="id"></param>
      /// <returns></returns>
         public bool updateZt(string num,string id)
         {
             SqlParameter[] parameters = {
                    new SqlParameter("@i", SqlDbType.VarChar,10),
					new SqlParameter("@id", SqlDbType.VarChar,10)
					};
             parameters[0].Value = num;
             parameters[1].Value = id;
             int i = 0;
             int ds = DbHelperSQL.RunProcedure("Up_updateIsShow", parameters, out i);
             if (i > 0)
             {
                 return true;
             }
             else
             {
                 return false;
             }
         }
      /// <summary>
      /// 得到产品广告
      /// </summary>
      /// <returns></returns>
         public DataSet getProjectList()
         {
             string sql = "select * from Advertisement where Imgtype=1 and isShow=1 order by Oby desc";
             DataSet dt = DbHelperSQL.GetDataSet(string.Format(sql));
             if (dt.Tables.Count > 0)
             {
                 return dt;
             }
             else
             {
                 return null;
             }
         }
         /// <summary>
         /// 得到品牌广告
         /// </summary>
         /// <returns></returns>
         public DataSet getPPList()
         {
             string sql = "select * from Advertisement where Imgtype=2 and isShow=1 order by Oby desc";
             DataSet dt = DbHelperSQL.GetDataSet(string.Format(sql));
             if (dt.Tables.Count > 0)
             {
                 return dt;
             }
             else
             {
                 return null;
             }
         }
     
    }
}
